package com.wanghua.spring.test.process;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

//数据读取核心类
public class ProcessOutputThread extends Thread {
    private InputStream is;
    private List<String> outputList;
    private Charset charset;

    public ProcessOutputThread(InputStream is,Charset charset) throws IOException {
        if (null == is) {
            throw new IOException("the provided InputStream is null");
        }
        this.is = is;
        this.charset=charset;
        this.outputList = new ArrayList<String>();
    }

    public List<String> getOutputList() {
        return this.outputList;
    }

    @Override
    public void run() {
        InputStreamReader ir = null;
        BufferedReader br = null;
        try {
            ir = new InputStreamReader(this.is, charset);
            br = new BufferedReader(ir);
            String line = null;
            while (null != (line = br.readLine())) {
                System.out.println(line);
                this.outputList.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != br) {
                    br.close();
                }
                if (null != ir) {
                    ir.close();
                }
                if (null != this.is) {
                    this.is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
